package fr.norsys.mapper.console.web;

import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.apache.struts.actions.DispatchAction;

import fr.norsys.mapper.console.model.Application;
import fr.norsys.mapper.console.model.Attribute;
import fr.norsys.mapper.console.model.ConsoleCst;
import fr.norsys.mapper.console.model.Resource;
import fr.norsys.mapper.console.service.ApplicationService;
import fr.norsys.mapper.console.service.MenuService;
import fr.norsys.mapper.console.service.ResourceService;

public class ResourceAction extends BaseAppDispatchAction {
	private static Log log = LogFactory.getLog(ResourceAction.class);

	private ApplicationService applicationService;

	private ResourceService resourceService;
	private MenuService menuService;

	public MenuService getMenuService() {
		return menuService;
	}

	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}

	/**
	 * Get a resource from the application.
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)  throws Exception {
		if (log.isDebugEnabled())
			{log.debug("edit method called");}
		Application currentApplication = (Application)request.getSession().getAttribute(ConsoleCst.CURRENT_APPLICATION);
		Resource currentResource = (Resource) request.getSession().getAttribute(ConsoleCst.CURRENT_RESOURCE);
		if(currentResource==null){
			currentResource = new Resource();
			request.getSession().setAttribute(ConsoleCst.CURRENT_RESOURCE,currentResource);
		}
		DynaActionForm resourceForm = (DynaActionForm) form;
		String currentResourceId = (String) request.getParameter(ConsoleCst.CURRENT_RESOURCE_ID);
		if (currentResourceId != null && !"".equals(currentResourceId)){
			resourceForm.set(ConsoleCst.RESOURCE_REQUEST_BEAN, currentResource);
			for(Iterator it=currentResource.getAttributes().iterator();it.hasNext();) {
				Attribute a = (Attribute) it.next();
				if("objectClass".equals(a.getAttributeLDAP()))
					request.setAttribute("objectClass",a.getDefaultValue());
			}
		}
		
		// put resources list in session to check the name of the new application to add
		// is not yet in the list
		request.getSession().setAttribute(ConsoleCst.RESOURCES_SESSION_BEAN,
				currentApplication.getResources());
		return mapping.findForward(ConsoleCst.RESOURCE_PAGE_FORWARD);
	}


	public void setApplicationService(ApplicationService applicationService) {
		this.applicationService = applicationService;
	}

	public ApplicationService getApplicationService() {
		return applicationService;
	}

	public void setResourceService(ResourceService resourceService) {
		this.resourceService = resourceService;
	}

	public ResourceService getResourceService() {
		return resourceService;
	}

}